package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.manor.ManorProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.manor.ManorMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_MANOR_HELP_RESP, desc = "跨服庄园互助玩家响应")
public class CrossManorHelpPlayerRespCmd extends AbstractCommand {
	ManorProto.CrossHelpPlayerRespMsg reqMsg;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = ManorProto.CrossHelpPlayerRespMsg.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ManorModule manorModule = player.getModule(ManorModule.class);
		manorModule.removeOpHelpUserId(reqMsg.getHelpUserId());
		Property consume = PropertyHelper.parseStringToProperty(GameConfig.MANOR_HEART_CONSUME);
		player.getModule(PreReduceModule.class).restorePreReduce(consume);
		if(reqMsg.getRet() == 0){
			player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.Manor, eLogMoneyType.ManorHelpPlayerCost);
			Property getReward = PropertyHelper.parseStringToProperty(reqMsg.getReward());
			player.getModule(CurrencyModule.class).addCurrency(getReward, eLogMoneyType.Manor, eLogMoneyType.ManorHelpPlayerGet);
			long yieldCount = 0L;
			for (BigInteger value : getReward.getGoods().values()) {
				yieldCount += value.longValue();
			}
			ManorMgr.addRankActivityYieldCount(player.getModule(ManorModule.class).getUserManorData(), yieldCount, player);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		ManorProto.HelpPlayerRespMsg.Builder respMsg = ManorProto.HelpPlayerRespMsg.newBuilder();
		respMsg.setRet(reqMsg.getRet());
		if(0 == reqMsg.getRet()) {
			respMsg.setReward(reqMsg.getReward());
			respMsg.setHelpCount(reqMsg.getHelpCount());
		}
		player.sendPacket(Protocol.U_MANOR_HELP_PLAYER, respMsg);
	}
}
